home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libsock / sync.c < prev    next >
C/C++ Source or Header  |  1997-05-23  |  6KB  |  179 lines

  1. /* sync.c:  Pilot synchronization logic
  2.  *
  3.  * Code written by Kenneth Albanowski, based on work apparently
  4.  * Copyright (C) 1996, U.S. Robotics Inc. (Possibly Copyright
  5.  * Palm Computing, Inc., or Copyright The Windward Group.)
  6.  *
  7.  * This file contains no information directly copied from the PC Conduit SDK,
  8.  * but paraphrases many of the algorithms used in that source.
  9.  *
  10.  */
  11.  
  12. /* Note: This file currently does nothing useful, and even if completed
  13.  * won't give you a full HotSync manager. 
  14.  * 
  15.  * Mostly I am including this as an exploration of the abstractions
  16.  * needed to portably sync local databases.
  17.  *
  18.  * Revised note: most of the algorithms needed are here, except for ones
  19.  * that checks the Pilot to decided whether slow or fast sync is needed, and
  20.  * to deal with category synchronization.
  21.  * Always use slow sync for now (and be sure to modify the Pilot's 
  22.  * PCID afterwards.)
  23.  *  
  24.  * Revised revision: all sections of code directly derived (albeit in a
  25.  * paraphased manner) from code in the Palm Conduit SDK, _which are not
  26.  * blatently obvious and irreducible_ have been temporarily removed until
  27.  * word is obtained from Palm that the code may be publically released.
  28.  *
  29.  * The issue that needs to be resolved is whether Palm Computing Inc. will
  30.  * allow the public distribution of source code derived from source code in
  31.  * their Pilot PC Conduit SDK. The licensing information that I recieved
  32.  * with the PC and Mac SDKs is not sufficient to resolve this question.
  33.  *
  34.  * In keeping with the sprit of the EU practice for reverse engineering, the
  35.  * interface that I have designed to plug into the Palm algorithms has been
  36.  * retained.
  37.  *
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include "pi-source.h"
  42. #include "pi-socket.h"
  43. #include "pi-dlp.h"
  44.  
  45. #define Abstract_sync
  46. #include "pi-sync.h"
  47.  
  48. /* Given a remote (Pilot) record, stored in a PilotRecord structure, determine what,
  49.    if anything, should be done with it, by looking at its flags, and possibly looking
  50.    it up in the local database. */
  51. int SyncRecord(int handle, int db, PilotRecord * Remote, struct SyncAbs * s, int slowsync) {
  52.   /* --- Paraphrased code derived from Palm;s Conduit SDK elided --- */
  53.   abort(); /* For lack of anything better to do */
  54.   return 0;
  55. }
  56.  
  57. /* Iterate over local records, copying records to remote, or deleting, or
  58.     archiving, as flags dictate. This is the last step in any sync. */
  59. void MergeToRemote(int handle, int db, struct SyncAbs * s) {
  60.   /* --- Paraphrased code derived from Palm's Conduit SDK elided --- */
  61.   abort(); /* For lack of anything better to do */
  62.   return;
  63. }
  64.  
  65. /* Perform a "slow" sync. This requires that the local (PC) has
  66.    consistent, accurate, and sufficient modification flags. All
  67.    of the records on the remote (Pilot) are pulled in, and compared
  68.    for modifications */
  69. int SlowSync(int handle, int db, struct SyncAbs * s ) {
  70.     int index = 0;
  71.     int retval = 0;
  72.     unsigned char buffer[0xffff];
  73.     PilotRecord p;
  74.     p.record = buffer;
  75.     
  76.       /* --- Paraphrased code derived from Palm's Conduit SDK elided --- */
  77.     index = 0;
  78.     
  79.     while(dlp_ReadRecordByIndex(handle,db, index, p.record, &p.ID, &p.length, &p.attr, &p.category)>=0) {
  80.         p.secret = p.attr & dlpRecAttrSecret;
  81.         p.archived = p.attr & dlpRecAttrArchived;
  82.         if(p.attr & dlpRecAttrDeleted)
  83.             p.attr = RecordDeleted;
  84.         else if (p.attr & dlpRecAttrDirty)
  85.             p.attr = RecordModified;
  86.         else
  87.             p.attr = RecordNothing;
  88.         SyncRecord(handle, db, &p, s, 1);
  89.         index++;
  90.     }
  91.     
  92.     MergeToRemote(handle,db,s);
  93.     
  94.     return retval;
  95. }
  96.  
  97. /* Perform a "fast" sync. This requires that both the remote (Pilot) and
  98.    local (PC) have consistent, accurate, and sufficient modification flags.
  99.    If this is not true, a slow sync should be used */
  100. int FastSync(int handle, int db, struct SyncAbs * s ) {
  101.     int index = 0;
  102.     int retval = 0;
  103.     unsigned char buffer[0xffff];
  104.     PilotRecord p;
  105.     p.record = buffer;
  106.     
  107.     while(dlp_ReadNextModifiedRec(handle,db, p.record, &p.ID, &index, &p.length, &p.attr, &p.category)>=0) {
  108.             printf("Got a modified record\n");
  109.         p.secret = p.attr & dlpRecAttrSecret;
  110.         p.archived = p.attr & dlpRecAttrArchived;
  111.         if(p.attr & dlpRecAttrDeleted)
  112.             p.attr = RecordDeleted;
  113.         else if (p.attr & dlpRecAttrDirty)
  114.             p.attr = RecordModified;
  115.         else
  116.             p.attr = RecordNothing;
  117.         SyncRecord(handle, db, &p, s, 0);
  118.     }
  119.     
  120.     MergeToRemote(handle,db,s);
  121.     
  122.     return retval;
  123. }
  124.  
  125. /* Overwrite remote (Pilot) with local (PC) records */
  126. int CopyToRemote(int handle, int db, struct SyncAbs * s) {
  127.     LocalRecord * Local = 0;
  128.     int retval = 0;
  129.     dlp_DeleteRecord(handle, db, 1, 0);
  130.     while(s->Iterate(s,&Local) && Local) {
  131.         if (Local->archived) {
  132.             retval = s->ClearStatusArchiveLocal(s,Local);
  133.             s->SetStatus(s,Local,RecordDeleted);
  134.         } else if (Local->attr != RecordDeleted) {
  135.             PilotRecord * p = s->Transmit(s,Local);
  136.             s->SetStatus(s,Local,RecordNothing);
  137.             p->attr = 0;
  138.             if(p->secret)
  139.                 p->attr |= dlpRecAttrSecret ;
  140.             retval = (dlp_WriteRecord(handle, db, p->attr, p->ID, 
  141.                         p->category, p->record, p->length, 0) < 0 );
  142.                 s->FreeTransmit(s,Local, p);
  143.         }
  144.     }
  145.     s->Purge(s);
  146.     return retval;
  147. }
  148.  
  149. /* Overwrite local (PC) with remote (Pilot) records. */
  150. int CopyFromRemote(int handle, int db, struct SyncAbs * s) {
  151.     unsigned char buffer[0xffff];
  152.     int index = 0;
  153.     PilotRecord p;
  154.     p.record = buffer;
  155.     s->DeleteAll(s);
  156.     while(dlp_ReadRecordByIndex(handle,db, index, p.record, &p.ID, &p.length, &p.attr, &p.category)>=0) {
  157.         p.secret = p.attr & dlpRecAttrSecret;
  158.         p.archived = p.attr & dlpRecAttrArchived;
  159.         if(p.attr & dlpRecAttrDeleted)
  160.             p.attr = RecordDeleted;
  161.         else if (p.attr & dlpRecAttrDirty)
  162.             p.attr = RecordModified;
  163.         else
  164.             p.attr = RecordNothing;
  165.         if( p.archived) {
  166.             p.attr = 0;
  167.             p.archived = 0;
  168.             s->ArchiveRemote(s,0,&p);
  169.         } else if (p.attr != RecordDeleted) {
  170.             p.attr = 0;
  171.             p.archived = 0;
  172.             s->StoreRemote(s,&p);
  173.         }
  174.         index++;
  175.     }
  176.     dlp_CleanUpDatabase(handle,db);
  177.     return 0;
  178. }
  179.